package com.wltt.product.ahuthr.service;

import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.enums.AttributeRemindEnum;
import com.wltt.product.ahuthr.enums.RemindStatusEnum;
import com.wltt.product.ahuthr.enums.RemindTypeEnum;
import com.wltt.product.ahuthr.mapper.RemindMapper;
import com.wltt.product.ahuthr.mapper.UserBasicMapper;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.bo.AgreementAttributeBO;
import com.wltt.product.ahuthr.model.bo.AgreementWithAttributeBO;
import com.wltt.product.ahuthr.model.po.RemindPO;
import com.wltt.product.ahuthr.model.po.UserBasicPO;
import com.wltt.product.ahuthr.model.vo.DealRemindReq;
import com.wltt.product.ahuthr.service.base.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @ClassName: RemindServiceImpl
 * @Description: 逻辑服务类<br />
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2024-08-18 16:26:22
 * @Author yanggl
 */
@Service(value = "remindService")
public class RemindServiceImpl extends BaseServiceImpl<RemindPO, Long> implements RemindService {

    private RemindMapper remindMapper;

    @Resource
    private UserBasicMapper userBasicMapper;

    //构造方法
    public RemindServiceImpl(RemindMapper remindMapper) {
        super(remindMapper);
        this.remindMapper = remindMapper;
    }

    /**
     * 创建合同相关提醒
     *
     * @param agreementWithAttributeBO
     * @return
     */
    @Override
    @Transactional
    public void createRemindForAgreement(AgreementWithAttributeBO agreementWithAttributeBO) {
        List<AgreementAttributeBO> agreementAttributeBOList = agreementWithAttributeBO.getAgreementAttributeBOList();

        UserBasicPO userBasicPO = null;

        for (AgreementAttributeBO agreementAttributeBO : agreementAttributeBOList) {
            if (AttributeRemindEnum.YES.getCode() != agreementAttributeBO.getAttributeRemind()) {
                continue;
            }

            // 提醒编码
            String remindCode = RemindTypeEnum.AGREEMENT_TYPE.getCode()
                    + "-" + agreementWithAttributeBO.getId()
                    + "-" + agreementAttributeBO.getId();

            RemindPO remindPO = remindMapper.queryByRemindCode(remindCode);
            if (null != remindPO) {
                return;
            }

            if (null == userBasicPO) {

                // TODO 排除非正常状态的人员
                userBasicPO = userBasicMapper.queryById(agreementWithAttributeBO.getUserId());
                if (null == userBasicPO) {
                    return;
                }
            }

            remindPO = new RemindPO();
            remindPO.setRemindType(RemindTypeEnum.AGREEMENT_TYPE.getCode());
            remindPO.setRemindCode(remindCode);

            // 提醒内容
            StringBuilder sb = new StringBuilder();
            sb.append(userBasicPO.getUserName());
            sb.append("(");
            sb.append(userBasicPO.getJobNumber());
            sb.append(")的\"");
            sb.append(agreementWithAttributeBO.getAgreementName());
            sb.append("\"中约定的\"");
            sb.append(agreementAttributeBO.getAttributeName());
            sb.append("\"到期提醒。");
            remindPO.setRemindContent(sb.toString());

            remindPO.setRemindDatetime(new Date());
            remindPO.setRemindTimes(1);
            remindPO.setRemindStatus(100);
            remindPO.setRoleId(agreementAttributeBO.getAttributeRemindRoleId());
            remindPO.setRelatedUserId(agreementWithAttributeBO.getUserId());
            remindPO.setRelatedObz(String.valueOf(agreementWithAttributeBO.getId()));
            remindMapper.insertSelective(remindPO);
        }
    }

    /**
     * 处理提醒
     *
     * @param dealRemindReq
     * @return
     */
    @Override
    public ResultData dealRemind(DealRemindReq dealRemindReq) {
        Long id = dealRemindReq.getId();
        if (null == id) {
            throw new BusinessException("未指定提醒", HttpStatus.BAD_REQUEST);
        }
        RemindPO remindPO = remindMapper.queryById(id);
        if (null == remindPO) {
            throw new BusinessException("提醒不存在", HttpStatus.BAD_REQUEST);
        }
        if (RemindStatusEnum.NEW.getCode() != remindPO.getRemindStatus()) {
            throw new BusinessException("状态禁止修改", HttpStatus.BAD_REQUEST);
        }

        remindPO.setRemindStatus(RemindStatusEnum.DEAL.getCode());
        remindMapper.updateById(remindPO);

        return ResultData.success();
    }
}